package cn.cs.controller;

import cn.cs.client.CsProportionSettingClient;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.pojo.CsProportionSetting;
import cn.cs.service.RepOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/repOrder")
public class RepOrderController {
    @Autowired
    private RepOrderService repOrderService;

    @Resource
    private CsProportionSettingClient proportionSettingClient;

    @RequestMapping("/getAllrepOrders")
    @ResponseBody
    public Dto getAllrepOrders(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getAllrepOrders(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取失败","002");
        }
        return dto;
    }

    /****
     * 根据维修单id获取订单详情
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getOrderInfoById")
    @ResponseBody
    public Dto getOrderInfoById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getOrderInfoById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取失败","002");
        }
        return dto;
    }


    /****
     * 查看所有维修订单评价接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getAllrepEva")
    @ResponseBody
    public Dto getAllrepEva(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getAllrepEva(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取失败","0002");
        }
        return dto;
    }


    /****
     * 添加配件接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/addRepProduct")
    @ResponseBody
    public Dto addRepProduct( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.addRepProduct(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","0002");
        }
        return dto;
    }


    /****
     * 生成账单
     * @param
     * @param
     * @return
     */
    @RequestMapping("/createBill")
    @ResponseBody
    public Dto createBill( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.createBill(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","0002");
        }
        return dto;
    }


    /****
     * 获取配件产品类型
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getRepProTypes")
    @ResponseBody
    public Dto getRepProTypes( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getRepProTypes(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","0002");
        }
        return dto;
    }


    /****
     *根据配件类型id获取配件产品类型
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getRepProTypesById")
    @ResponseBody
    public Dto getRepProTypesById( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getRepProTypesById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","0002");
        }
        return dto;
    }


    /****
     * 扫描二维码接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/ScanQrCode")
    @ResponseBody
    public Dto ScanQrCode(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.ScanQrCode(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 创建检测报告
     * @param
     * @param
     * @return
     */
    @RequestMapping("/createTestReport")
    @ResponseBody
    @Transactional
    public Dto createTestReport(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.createTestReport(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 查看检测报告
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getTestReportById")
    @ResponseBody
    public Dto getTestReportById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getTestReportById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 查看账单
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getBillById")
    @ResponseBody
    public Dto getBillById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getBillById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 候保产品订单查询
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getHbRepOrderList")
    @ResponseBody
    public Dto getHbRepOrderList(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getHbRepOrderList(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }


    /****
     * 候保产品订单查询
     * @param
     * @param
     * @return
     */
    @RequestMapping("/delRepProduct")
    @ResponseBody
    public Dto delRepProduct(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.delRepProduct(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 查看维修单评价接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getRepOrderEvlById")
    @ResponseBody
    public Dto getRepOrderEvlById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=repOrderService.getRepOrderEvlById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 查看检测报告  微信公众号
     * @return
     */
    @RequestMapping("/wxApi/getWxTestReportById")
    @ResponseBody
    public Dto getWxTestReportById(String orderId) throws  Exception{
        return repOrderService.getWxTestReportById(orderId);
    }


    /**
     * 查询维修工抽成方案
     * @return
     */
    @RequestMapping("/p/getRepairPercentage")
    @ResponseBody
    public Dto getRepairPercentage()throws Exception{
        List<CsProportionSetting> list = new ArrayList<>();
        list.add(proportionSettingClient.getCsProportionSettingById("1"));
        return DtoUtil.returnSuccess("获取成功", list);
    }

    /**
     * 查询资金池比例
     * @return
     */
    @RequestMapping("/p/getCapitalPool")
    @ResponseBody
    public Dto getCapitalPool()throws Exception{
        return DtoUtil.returnSuccess("获取成功", proportionSettingClient.getCsProportionSettingById("1"));
    }


    /**
     * 修改维修工抽成方案
     * @param proportionPlatform 平台抽成
     * @param proportionProvince 省代理抽成（一级代理）
     * @param proportionCity 市代理抽成（二级代理）
     * @return
     * @throws Exception
     */
    @RequestMapping("/csPlatform/updateRepairPercentage")
    @ResponseBody
    public Dto updateRepairPercentage(String proportionPlatform, String proportionProvince, String proportionCity, String capitalPool, String productServiceCharge, String insurancePremium)throws Exception{
        CsProportionSetting proportionSetting = new CsProportionSetting();
        proportionSetting.setProportionPlatform(proportionPlatform);
        proportionSetting.setProportionProvince(proportionProvince);
        proportionSetting.setProportionCity(proportionCity);
        proportionSetting.setCapitalPool(capitalPool);
        proportionSetting.setProductServiceCharge(productServiceCharge);
        proportionSetting.setInsurancePremium(insurancePremium);
        proportionSetting.setId(1);

        return DtoUtil.returnSuccess("修改成功", proportionSettingClient.updateCsProportionSetting(proportionSetting));
    }


}
